# coding:utf-8

import os, sys, time
import sqlite3 as sql
import time


class Dbapi(object):
    def __init__(self, dbname):
        self.connection = sql.connect(dbname)
        self.cursor = self.connection.cursor()
        
    def close(self):
        try:
            self.cursor.close()
            self.connection.close()
        except:
            pass
        
    def __del__(self):
        self.close()
        
    def execSql(self, *args, **kw):
        try:
            self.cursor.execute(*args, **kw)
            self.connection.commit()
        except sql.Error, e:
            import report
            report.error("sql error[%s]:%s"%(e, (args, kw)))
            
    def getResult(self, nums=None):
        if nums is None:
            return self.cursor.fetchall()
        return self.cursor.fetchmany(nums)
    
def zip(tuples):
    return (',%s'*len(tuples)%tuple(tuples))[1:]

def qmark(tuples):
    return (',?'*len(tuples))[1:]

def zip_qmark(tuples):
    return (',%s=?'*len(tuples)%tuple(tuples))[1:]
    
class BaseInfo(object):
    """
    Base class about database operation.
    """
    conn = None
    def setConnection(self, connection):
        self.conn = connection

    def getConnection(self):
        return self.conn
    
    def getResult(self, *args):
        return self.conn.getResult(*args)
        
    def create(self, tablename, *columns):
        cols = zip(columns)
        return self.conn.execSql("CREATE TABLE IF NOT EXISTS %s(%s)"%(tablename, cols))
    
    def insert(self, tablename, **key_values):
        columns = key_values.keys()
        values = key_values.values()
        s = "INSERT INTO %s(%s) VALUES(%s)"%(tablename, zip(columns), qmark(values))
        return self.conn.execSql(s, values)
    
    def update(self, tablename, where, **key_values):
        columns = key_values.keys()
        values = key_values.values()
        if where:
            s = "UPDATE %s SET %s WHERE %s"%(tablename, zip_qmark(columns), where)
        else:
            s = "UPDATE %s SET %s"%(tablename, zip_qmark(columns))
        return self.conn.execSql(s, values)
    
    def select(self, tablename, where, *columns):
        if len(columns) == 0:
            columns = ('*',)
        if where:
            s = 'SELECT %s FROM %s WHERE %s'%(zip(columns), tablename, where)
        else:
            s = 'SELECT %s FROM %s'%(zip(columns), tablename)
        return self.conn.execSql(s)
    
    def delete(self, tablename, where):
        self.conn.execSql("DELETE FROM %s WHERE %s"%(tablename, where))
        
    def wrapper(self):
        def generator():
            while True:
                try:
                    yield self.getResult(1)[0]
                except:
                    break
        return generator()
                
    


class VehicleInfo(BaseInfo):
    vehilceTable = "vehicleinfo"
    def __init__(self, conn):
        self.setConnection(conn)
        self.create(self.vehilceTable, "vehicle")
    
    
    def readVehicle(self):
        self.select(self.vehilceTable, "", "vehicle")
        return self.getResult()
    
    def writeVehicle(self, tel):
        self.insert(self.vehilceTable, vehicle=tel)
        
    def deleteVehicle(self, tel):
        self.delete(self.vehilceTable, "vehicle='%s'"%tel)
        
    

if __name__ == '__main__':
    pass


                